
global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/spillover_weights.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"

capture noi {


/* This do-file builds the firm-level weights weighted 
by unique country of inventor for the spillover variables */


***********************************************************************************************************************
***	 Build spillovers weights using all patents
***	 ORBIS 2017
***********************************************************************************************************************
qui do ${code_dir}/config/country_list.do

*load firms and their patent applications
use ${dataset_dir}/patstat_orbis/Orbis_patents_list_2017_merged.dta, clear
keep BvD appln_id

*load complete inventor country set, some are imputed
mmerge appln_id using ${commondata_dir}/patstat_2018b/appln_inventor_country_nomissing.dta, unmatched(none)
drop _m
*correct GER/GDR and RUS/USSR
do ${code_dir}/config/auth_map.do invt_country

*load patent family identifier and first filing year
mmerge appln_id using ${commondata_dir}/patstat_2018b/family_info.dta, unmatched(master) ukeep(docdb_family_id fam_earliest_appln_year)

*go to unique inventor countries per patent family
drop appln_id _m
duplicates drop

*sum up unique inventor countries per BvD and patent family and attribute them equally to te country
ren fam_earliest_appln_year fam_year
gen byte x=1
bysort docdb BvD : egen nb_invt_ctries = sum(x)
gen inv_nb_invt_ctries = 1/nb_invt_ctries
drop nb_invt_ctries x
*make dummies for each inventor country with the patents attributed inventors to that country
foreach ctry in $invtcountrylist {
	gen invt_`ctry' = invt_country=="`ctry'"
	replace invt_`ctry' = invt_`ctry' * inv_nb_invt_ctries
}
compress
ren fam_year year

*sum up on a firm year level. These are our inventorcounts for a firm in a given year. They are NOT normalized to 1 yet!
foreach ctry in $invtcountrylist {
	bysort BvD year: egen nb_pat_invt_`ctry' = sum(invt_`ctry')
	drop invt_`ctry'
}

keep BvD year nb_pat_invt_*
duplicates drop

*labeling

compress
save ${dataset_dir}/weights/bvdid_inventorweights_Orbis2017.dta, replace

**************************************************************************************************************************************
*	Weights
**************************************************************************************************************************************

* Use all patents from 1970 weights for the window to 1989
use ${dataset_dir}/weights/bvdid_inventorweights_Orbis2017.dta, clear
drop if year == 9999
keep if year > 1970
keep if year < 1995-5
drop year

*sum up across entire period for every country per bvd
collapse (sum) nb_pat_invt_*, by(BvD)
egen total_pat = rowtotal(nb_pat_invt_??), missing
keep if total_pat > 0

*generate shares
foreach ctry in $invtcountrylist {
	gen share_invt_1995_`ctry' = nb_pat_invt_`ctry' / total_pat
}
* obviously some shares are missing for all countries for some BVDIDs. we attribute average distribution of inventions worldwide in that period later on in the code
keep BvD share*

*labeling

compress
save ${dataset_dir}/weights/bvdid_inventor_weights_all_1970t1989_1995_orbis2017.dta, replace


* Use all patents from 1970 weights
use ${dataset_dir}/weights/bvdid_inventorweights_Orbis2017.dta, clear
drop if year == 9999
keep if year > 1970
keep if year < 1995
drop year

*sum up across entire period for every country per bvd
collapse (sum) nb_pat_invt_*, by(BvD)
egen total_pat = rowtotal(nb_pat_invt_??), missing
keep if total_pat>0

*generate shares
foreach ctry in $invtcountrylist {
	gen share_invt_1995_`ctry' = nb_pat_invt_`ctry' / total_pat
}
* obviously some shares are missing for all countries for some BVDIDs. we attribute average distribution of inventions worldwide in that period later on in the code
keep BvD share*

*labeling

compress
save ${dataset_dir}/weights/bvdid_inventor_weights_all_from1970_1995_orbis2017.dta, replace


* Shares patents Orbis 2017, make a complete set of shares for all countries
use ${dataset_dir}/weights/bvdid_inventorweights_Orbis2017.dta, clear
drop if year == 9999
drop if year >= 1995
drop if year < 1995-10
drop year
* sum up across entire period for every country per bvd
collapse (sum) nb_pat_invt_*, by(BvD)

*generate missing observations as zeros
foreach ctry of global countrylist1995 {
	cap ren nb_pat_invt_`ctry' invt_share_`ctry'
	if _rc != 0 {
		gen invt_share_`ctry' = 0.0
	}
}
drop nb_pat_invt_??
	
egen total_pat = rowtotal(invt_share_??), missing
keep if total_pat > 0

*generate shares
foreach ctry of global countrylist1995 {
	gen share_invt_1995_`ctry' = invt_share_`ctry' / total_pat
	ren share_invt_1995_`ctry' share2_all_1995_`ctry'
}
keep BvD share*

*labeling

compress
save ${dataset_dir}/weights/bvdid_pat_weights_EPtr_from1970_1995_orbis2017_iw.dta, replace
 


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat